package com.org.Bibliotech;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MyUnit {

    private static Library library;
    private Customer currentUser = null;
    private ArrayList<Customer> registry = null;
    public static void main(String[] args) {

        MyUnit myUnit = new MyUnit();
        myUnit.createLibrary();
        myUnit.createCustomers();

        System.out.println("Welcome, User! Please login. ");
        myUnit.login();
        myUnit.renderMainMenu();


    }

    public void login(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String loginNumber = null, password = null;
        try {

            System.out.print("Please enter your login number XXX-XXXX: ");
            loginNumber = reader.readLine();

            System.out.print("Please enter your password: ");
            password = reader.readLine();

        }catch(IOException e){
            System.out.println("There was a problem reading your input. Please tray again.");
            login();
            return;
        }
        currentUser = getCustomer(loginNumber, password);

       if(currentUser == null){
           System.out.println("That information didn't match anything in our database. Please try again");
           login();
           return;
       }
        System.out.println("Welcome to Biblioteca, " + currentUser.getName() + '!');





    }


    public Customer getCustomer(String loginNumber, String password){
        for(Customer customer : registry)
            if(customer.isMatch(loginNumber, password))
                return customer;

        return null;
    }



    public void createLibrary(Library library){
        this.library = library;
    }

    public void createLibrary(){

        Book HeadFirstJava = new Book("Head First Java", "Kathy Sierra & Bert Bates", 2003);
        Book AnnaKarenina = new Book("Anna Karenina", "Leo Tolstoy", 1877);
        ArrayList<Book> books = new ArrayList<Book>();

        books.add(HeadFirstJava);
        books.add(AnnaKarenina);

        Movie schindlersList = new Movie("Schindler's List", 1993, "Steven Spielberg", 10);
        Movie amadeus = new Movie ("Amadeus", 1984, "Milos Forman", 10);

        ArrayList<Movie> movies = new ArrayList<Movie>();
        movies.add(schindlersList);
        movies.add(amadeus);

        library = new Library(books, movies);
    }


    public void createCustomers(){

        registry = new ArrayList<Customer>();
        registry.add(new Customer("Josh", "123-4567", "password", "example@example.net", "123-4567" ));

    }

    public void renderMainMenu() {

        System.out.println("Main menu\n");

        System.out.println("<======================>");
        System.out.println("Please select a number");

        System.out.println("0. Exit");
        System.out.println("1. See Available Books");
        System.out.println("2. Return a Book");
        System.out.println("3. See Available Movies");
        System.out.println("4. Check Book Status");
        System.out.println("5. Get Your Details");
        System.out.println("<======================>");

        System.out.println();

        String command = getCommand();
        executeCommand(command);


    }

    public String getCommand(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


        System.out.print("Please enter your choice: ");
        String command = null;

        try{
            command = reader.readLine();
        } catch(IOException e){
            System.out.println("Couldn't read the command.");
            e.printStackTrace();
        }

        System.out.println();
        return command;
    }

    public void executeCommand(String _command){

        int command = -1;

        command = Integer.parseInt(_command);

        switch(command){

            case 0: prepExit(); break;

            case 1: accessBooks();             break;



            case 2: returnBook(); return;

            case 3: listMovies();
                System.out.println("Enter the number of the movie you'd like to check out or 0 to leave");
                int movieToGet = Integer.parseInt(getCommand());

                if(movieToGet == 0)
                    prepExit();

                else checkoutMovie(movieToGet);
                break;

            case 4: checkBookStatus();
                     break;


            case 5: printCustomerDetails();
                break;



            default: System.out.println("Select a valid option!");
                     executeCommand(getCommand());



        }

        renderMainMenu();
    }

    public void printCustomerDetails(){
        System.out.println(currentUser.getDetails());
    }

    public void accessBooks(){
        listBooks();
        System.out.println("Enter the number of the book you'd like to check.");
        System.out.println("0 will exit the program. Entering no book will send you back to the menu.");
        String command = getCommand();


        if(command.equals("")){
            return;
        }
        int bookToGet = Integer.parseInt(command);

        if(bookToGet == 0)
            prepExit();

        else
            checkoutBook(bookToGet);
    }

    public void prepExit(){

        System.out.println("Are you sure you would like to exit? Y/N");
        char command = getCommand().toUpperCase().charAt(0);

        if(command == 'Y')
            System.exit(0);


    }

    public void checkBookStatus(){
        System.out.println("Which book are you interested in?");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String bookTitle = null;
        try{
            bookTitle = reader.readLine();
        } catch (IOException e){
            System.out.println("We didn't get that book. Please try again.");
            checkBookStatus();
        }

        ArrayList<Book> checkedOutBooks = library.getCheckedOutBooks();

        if (bookTitle.equals(null)){
            System.out.println("Sorry, we didn't get that. Please try again.");
            checkBookStatus();
        }
        for(Book book : checkedOutBooks) {

            if (book.getTitle().equals(bookTitle)) {
                System.out.println(bookTitle + " is owned by: " + book.getOwner().getName());
                return;
            }
        }

        System.out.println("I'm sorry, we couldn't find " + bookTitle + " It is not currently checked out.");

    }
    public void listBooks(){
        ArrayList<Book> books = library.getAvailableBooks();

        if(books.size() == 0){
            System.out.println("All of our books our checked out. ");
            renderMainMenu();
            return;
        }


        int bookNumber = 1;
        for(Book book : books){

            System.out.println(bookNumber + ".");
            System.out.println(book.getDetails());
            System.out.println();

            bookNumber++;

        }
    }


    public void listMovies(){
        ArrayList<Movie> movies = library.getAvailableMovies();

        if(movies.size() == 0){
            System.out.println("All of our movies our checked out. ");
            renderMainMenu();
            return;
        }


        int movieNumber = 1;
        for(Movie movie : movies){

            System.out.println(movieNumber + ".");
            System.out.println(movie.getDetails());
            System.out.println();

            movieNumber++;

        }
    }



    public void returnBook(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

         String title = null;
        String author = null;
         int publicationYear = -1;

         try {
             System.out.print("Please enter the title of the book: ");
              title = reader.readLine();

             System.out.print("Please enter the author(s) of the book: ");
              author = reader.readLine();

             System.out.print("Please enter year the book was published of the book: ");
              publicationYear = Integer.parseInt(reader.readLine());
         } catch(IOException e){
             System.out.println("Trouble creating the book. Please try again");
             returnBook();
         }

        if(!title.equals(null) && !author.equals(null) && publicationYear != -1)
            System.out.println(returnBook(new Book(title, author, publicationYear)));

        else System.out.println("Something's gone wrong.");
         renderMainMenu();
    }

    public String returnBook(Book book){

        if(library.returnBook(book))
            return "Thank you for returning the book.";

        else return "That is not a valid book to return.";
    }


    public void checkoutBook(int bookToGet){


        try {

            Book bookToCheckOut = (Book)library.getAvailableBooks().get(bookToGet-1);
            bookToCheckOut.setOwner(currentUser);

            System.out.println(checkOut(bookToCheckOut));
            System.out.println();

            //renderMainMenu();

        }catch(IndexOutOfBoundsException e){
            System.out.println("That wasn't a valid book! Please enter a valid book!");

            int newBook = Integer.parseInt(getCommand());
            checkoutBook(newBook);
        }


    }

    public void checkoutMovie(int movieToGet){


        try {
            Movie movieToCheckOut = (Movie)library.getAvailableMovies().get(movieToGet-1);

            System.out.println(checkOut(movieToCheckOut));
            System.out.println();

            //renderMainMenu();
        }catch(IndexOutOfBoundsException e){
            System.out.println("That wasn't a valid book! Please enter a valid book!");

            int newMovie = Integer.parseInt(getCommand());
            checkoutMovie(newMovie);
        }


    }

    public String checkOut(Movie movie){

        if(library.checkoutMovie(movie))
            return "Thank you! Enjoy the movie";

        else return "That movie is not available.";
    }

    public String checkOut(Book book){

        if(library.checkoutBook(book))
            return "Thank you! Enjoy the book";

        else return "That book is not available.";
    }


}
